/**
 * 
 */
package com.jolestar.yayaweibotong.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.users.UserServiceFactory;
import com.jolestar.yayaweibotong.dao.UserDomain;
import com.jolestar.yayaweibotong.service.UserService;

/**
 * @author jolestar@gmail.com
 * 
 */
public class LoginAction extends AbstractAction {

	/**
	 * 
	 */
	public LoginAction() {
		super("login");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jolestar.yayaweibotong.controller.WebAction#doAction(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public String doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (this.isLogin(request)) {
			return "redirect:/dashboard";
		}
		String service = request.getParameter("auth");

		if ("google".equals(service)) {
			com.google.appengine.api.users.UserService googleUserService = UserServiceFactory
					.getUserService();
			String thisURL = request.getRequestURI();
			if (request.getUserPrincipal() != null) {
				String email = request.getUserPrincipal().getName();
				UserDomain user = UserService.ServiceHolder.getInstance()
						.getUser(email);
				if (user == null) {
					return "redirect:/register-by-google";
				}
				request.getSession(true).setAttribute("logout_url",
						googleUserService.createLogoutURL(thisURL));
				return this.login(user, request);
			} else {
				return "redirect:"
						+ googleUserService.createLoginURL(thisURL
								+ "?auth=google");
			}
		}
		if (request.getMethod().equalsIgnoreCase("post")) {
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			request.setAttribute("email", email == null ? "" : email);
			request.setAttribute("password", password == null ? "" : password);
			if (StringUtils.isBlank(email) || StringUtils.isBlank(password)) {
				request.setAttribute("error", "请填写完整.");
				return "login";
			}
			UserDomain user = UserService.ServiceHolder.getInstance().getUser(
					email);
			if (user == null) {
				request.setAttribute("error",
						"该邮箱尚未注册。请检查 或者 点击<a href=\"/register\">这里</a>注册.");
				return "login";
			}
			if (!user.getPassword().equals(password)) {
				request.setAttribute("error", "密码错误.");
				return "login";
			}
			return this.login(user, request);
		}
		return "login";
	}

	private boolean isLogin(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return session != null && session.getAttribute("user") != null;
	}

	private String login(UserDomain user, HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		session.setAttribute("user", user);
		return "redirect:/dashboard";
	}

	public String getPath() {
		return "/" + this.getName();
	}

}
